package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class SetmealServiceImpl implements SetmealService {

    private final SetmealDishMapper setmealDishMapper;
    private final SetmealMapper setmealMapper;

    /**
     * 新增菜单
     */
    @Transactional
    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.save(setmeal);
        Long setmealId = setmeal.getId();

        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        list.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
            System.out.println(setmealDish);
        });
        setmealDishMapper.save(list);
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page =  setmealMapper.page(setmealPageQueryDTO);
        System.out.println(page);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());
        return pageResult;
    }

    /**
     * 根据ID进行查询
     * @param id
     * @return
     */
    @Transactional
    @Override
    public SetmealVO findById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        Setmeal setmeal = setmealMapper.findById(id);

        BeanUtils.copyProperties(setmeal,setmealVO);
        List<SetmealDish> setmealDishes = setmealDishMapper.getSetMealIdByDishIdForSetmealDish(id);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }
    /**
     * 停售起售状态
     * @param status
     * @param id
     */

    @Override
    public void updateStatus(Long status, Integer id) {
        setmealMapper.updateStatus(status,id);
    }
    /**
     * 批量删除
     * @param ids
     */
    @Transactional
    @Override
    public void delete(List<Long> ids) {
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.findById(id);
            if (setmeal.getStatus() == 1) {
                // 起售中的套餐不可删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

        ids.forEach(id -> {
            // 删除菜单数据
            setmealMapper.delete(id);
            // 删除关系表的数据
            setmealDishMapper.delete(id);

        });

        
    }

    /**
     * 更新数据
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        // 1.更新基本数据
        setmealMapper.update(setmeal);
        Long setmeal_id = setmeal.getId();
        // 2.先删除setmeal_dish表中的关联数据后
        setmealDishMapper.delete(setmeal.getId());
        // 3。再重新插入数据
        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        list.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal_id);
        });
        setmealDishMapper.save(list);
    }
}
